Specifies a subroutine to run automatically when the script exits.
OnExit [, Label]
Label | If omitted, the script is returned to its normal exit behavior. Otherwise, specify the name of the label whose contents will be executed (as a new thread) when the script exits by any means. |
IMPORTANT: Since the specified subroutine is called instead of terminating the script, that subroutine must use the ExitApp command if termination is desired. Alternatively (as in the Examples section below), the OnExit subroutine might display a MsgBox to prompt the user for confirmation -- and only if the user presses YES would the script execute ExitApp to close itself.
The OnExit subroutine is called when the script exits by any means (except when it is killed by something like "End Task"). It is also called whenever the #SingleInstance and Reload commands ask a previous instance to terminate.
A script can detect and optionally abort a system shutdown or logoff via OnMessage(0x11, "WM_QUERYENDSESSION").
The OnExit thread does not obey #MaxThreads (it will always launch when needed). In addition, while it is running, it cannot be interrupted by any thread, including hotkeys, custom menu items, and timed subroutines. However, it will be interrupted (and the script will terminate) if the user chooses Exit from the tray menu or main menu, or the script is asked to terminate as a result of Reload or #SingleInstance. Because of this, the OnExit subroutine should be designed to finish quickly unless the user is aware of what it is doing.
If the OnExit thread encounters a failure condition such as a runtime error, the script will terminate. This prevents a flawed OnExit subroutine from making a script impossible to terminate.
If the OnExit subroutine was launched due to an Exit or ExitApp command that specified an exit code, that code is ignored and no longer available. A new exit code can be specified by the OnExit subroutine if/when it calls ExitApp.
Whenever the OnExit subroutine is called by an exit attempt, it starts off fresh with the default values for settings such as SendMode. These defaults can be changed in the auto-execute section.
The built-in variable A_ExitReason is blank unless the OnExit subroutine is currently running or has been called at least once by a prior exit attempt. If not blank, it is one of the following words:
Logoff | The user is logging off. |
Shutdown | The system is being shut down or restarted, such as by the Shutdown command. |
Close | The script was sent a WM_CLOSE or WM_QUIT message, had a critical error, or is being closed in some other way. Although all of these are unusual, WM_CLOSE might be caused by WinClose having been used on the script's main window. To prevent this, dismiss the main window with Send, !{F4}. |
Error | A runtime error occurred in a script that has no hotkeys and that is not persistent. An example of a runtime error is Run/RunWait being unable to launch the specified program or document. |
Menu | The user selected Exit from the main window's menu or from the standard tray menu. |
Exit | The Exit or ExitApp command was used (includes custom menu items). |
Reload | The script is being reloaded via the Reload command or menu item. |
Single | The script is being replaced by a new instance of itself as a result of #SingleInstance. |
OnMessage(), OnClipboardChange, ExitApp, Shutdown, #Persistent, Threads, Gosub, Return, Menu
#Persistent ; For demonstration purposes, keep the script running if the user chooses "No". OnExit, ExitSub return ExitSub: if A_ExitReason not in Logoff,Shutdown ; Avoid spaces around the comma in this line. { MsgBox, 4, , Are you sure you want to exit? IfMsgBox, No return } ExitApp ; The only way for an OnExit script to terminate itself is to use ExitApp in the OnExit subroutine.